package com.zteits.gov.monitor.websocket.task;

import com.zteits.gov.monitor.websocket.domain.Greeting;
import com.zteits.gov.monitor.websocket.utils.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Copyright: Copyright (c) 2018 zteits
 *
 * @ClassName: TopicTask.java
 * @Description:
 * @version: v1.0.0
 * @author: jingjq
 * @date: 2018年09月04日 10:24
 */
@Component
@EnableScheduling
public class TopicTask {

    @Autowired
    private SimpMessagingTemplate template;

    @Scheduled(fixedRate = 1000)
    public void callback() {
        String[] targets = {"1001", "1002", "1003"};
        for (String target : targets) {
            Greeting greeting = new Greeting();
            greeting.setTarget(target);
            greeting.setContent("[" + JSONUtil.toJSONString(new Date()) + "]");
            this.template.convertAndSend("/topic/feed." + target, greeting);

            try {
                Thread.sleep(500); // simulated delay
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
